﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Maticsoft.DBUtility;
using MySql.Data.MySqlClient;
using System.Collections;
using EnvironmentManagerSystem.MODE;

namespace EnvironmentManagerSystem.BLL
{
    public class Function
    {
        /// <summary>
        /// 获取功能列表
        /// </summary>
        /// <returns></returns>
        public static functionDataCollection GetList()
        {
            try
            {
                functionDataCollection list = new functionDataCollection();
                string strSql = @"SELECT * FROM `tbs001_function`";
                using (MySqlDataReader dr = DbHelperMySQL.ExecuteReader(strSql))
                {
                    while (dr.Read())
                    {
                        functionData model = new functionData();
                        model.id = (int)dr["ID"];
                        model.name = dr["name"].ToString();
                        list.Add(model);
                    }
                }
                return list;
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// 获取功能列表
        /// </summary>
        /// <returns></returns>
        public static functionDataCollection GetList(int RoleID)
        {
            try
            {
                functionDataCollection list = new functionDataCollection();
                string strSql = @"SELECT c.NAME,c.id FROM tbs001_q a,tbs001_role b,tbs001_function c WHERE a.RoleID=b.ID AND a.FunID=c.id AND b.ID=" + RoleID;
                using (MySqlDataReader dr = DbHelperMySQL.ExecuteReader(strSql))
                {
                    while (dr.Read())
                    {
                        functionData model = new functionData();
                        model.id = (int)dr["ID"];
                        model.name = dr["name"].ToString();
                        list.Add(model);
                    }
                }
                return list;
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// 删除角色权限
        /// </summary>
        /// <param name="RoleID"></param>
        /// <param name="FunctionID"></param>
        /// <returns></returns>
        public static bool DeleteRoleFunction(int RoleID, int FunctionID)
        {
            try
            {
                string strSql = string.Format(@"delete from `TBS001_q` where RoleID={0} and FunID={1}", RoleID,FunctionID);
                return DbHelperMySQL.ExecuteSql(strSql) > 0 ? true : false;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 添加角色权限
        /// </summary>
        /// <param name="RoleID"></param>
        /// <param name="FunctionID"></param>
        /// <returns></returns>
        public static bool InsertRoleFunction(int RoleID, int FunctionID)
        {
            try
            {
                string strSql = string.Format(@"INSERT INTO `TBS001_q` (RoleID,FunID,value) VALUES ({0},{1},1)", RoleID, FunctionID);
                return DbHelperMySQL.ExecuteSql(strSql) > 0 ? true : false;
            }
            catch
            {
                return false;
            }
        }
    }
}
